<?php


namespace App\backend\services;

use App\backend\models\SettingCustomerServiceModel;

class SettingCustomerServiceServices
{

    /**
     * 列表
     */
    public static function getPageList($params)
    {
        $keyword = isset($params['keyword'])?trim($params['keyword']):'';
        $where = [];
        if ($keyword){
            $where[] = ['wechatNum','like','%'.$keyword.'%'];
        }
        $page = !isset($params['page']) ? 1 : $params['page'];unset($params['page']);
        $limit = !isset($params['limit']) ? 10 : $params['limit'];unset($params['limit']);

        $result = SettingCustomerServiceModel::select(['id','wechatNum','status'])
            ->where($where)
            ->paginate($limit, ['*'], 'page', $page)->toArray();
        foreach ($result['data'] as $k=>&$v){
            $v = $v?str_replace('null','',$v):'';
        }
        return ['data'=>$result['data'],'total'=>$result['total']];

    }

    /**
     * 添加/编辑
     *
     */
    public static function modify($params)
    {
        if (empty($params)) {
            return [];
        }

        $id = isset($params['id']) && !empty($params['id']) ? $params['id'] : 0;unset($params['id']);

        if ($id) {
            $params['updated_at'] = time();
            $result = SettingCustomerServiceModel::where('id',$id)->update($params);
        } else {
            $params['created_at'] = time();
            $result = SettingCustomerServiceModel::insert($params);
        }

        return $result?$result:'';
    }

    /**
     * 删除
     */
    public static function delete($params)
    {
        $id = $params['id'];
        if (empty($id)) {
            return [];
        }

        return SettingCustomerServiceModel::where('id',$id)->delete();
    }

}
